package agend.client.gui.panels.agend;
import jade.gui.GuiEvent;

import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.ListModel;

import GoogleServices.GoogleCalendarCalls;
import agend.client.gui.panels.agend.AgendPanel.MyEvent;
import agend.client.gui.utils.Broker;
import agend.client.gui.utils.HandlerEvent;
import agend.client.gui.utils.HandlerView;

import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.EventWho;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DialogCalendarEventPanel extends javax.swing.JDialog  {

	/**
	 * Auto-generated main method to display this JDialog
	 */

	private JLabel labelDescrizione;
	private JButton deleteButton;
	private JSeparator jSeparator4;
	private JScrollPane jScrollPane;
	private JLabel panelTitleLabel;
	private JSeparator jSeparator3;
	private JSeparator jSeparator2;
	private JSeparator jSeparator1;
	private JSeparator jSeparator;
	private JList<String> invitatiList;
	private JTextArea descrizioneTextArea;
	private JLabel doveField;
	private JLabel titoloField;
	private JLabel labelInvitati;
	private JLabel labelDove;
	private JLabel labelOrari;
	private JLabel labelTitolo;
	private DetailsPanel details;
	private MyEvent event;
	private ListModel invitatiListModel;


	public MyEvent getEvent() {
		return event;
	}

	public void setEvent(MyEvent event) {
		this.event = event;
	}

	public DialogCalendarEventPanel(Frame frame, MyEvent event) {
		super(frame,true);

		initGUI();
		addListener();
		setEvent(event);
		setFieldsFromCalendarEvent(event);

	}

	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.columnWidths = new int[] {7, 7};
				thisLayout.columnWeights = new double[] {0.1, 0.1};
				thisLayout.rowWeights = new double[] {0.1, 0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 0.1, 0.0};
				thisLayout.rowHeights = new int[] {20, 7, 7, 20, 7, 20, 7, 20, 7, 20, 7, 20, 20, 20, 7, 20, 7};
				getContentPane().setLayout(thisLayout);
				getContentPane().setBackground(new java.awt.Color(255,255,255));

				{
					labelTitolo = new JLabel();
					getContentPane().add(labelTitolo, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelTitolo.setText("Titolo Evento");
					labelTitolo.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					labelOrari = new JLabel();
					getContentPane().add(labelOrari, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelOrari.setText("Giorno e Ora");
					labelOrari.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					labelDove = new JLabel();
					getContentPane().add(labelDove, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelDove.setText("Dove");
					labelDove.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					labelDescrizione = new JLabel();
					getContentPane().add(labelDescrizione, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelDescrizione.setText("Descrizione");
					labelDescrizione.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					labelInvitati = new JLabel();
					getContentPane().add(labelInvitati, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					labelInvitati.setText("Invitati   ");
					labelInvitati.setFont(new java.awt.Font("Segoe UI",1,12));
				}
				{
					titoloField = new JLabel();
					getContentPane().add(titoloField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					titoloField.setText("aaa");
				}
				{
					doveField = new JLabel();
					getContentPane().add(doveField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

				}
				{
					descrizioneTextArea = new JTextArea();
					getContentPane().add(descrizioneTextArea, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

				}
				
				{
					details = new DetailsPanel();
					getContentPane().add(details, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

				}
				{
					jSeparator = new JSeparator();
					getContentPane().add(jSeparator, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jSeparator1 = new JSeparator();
					getContentPane().add(jSeparator1, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jSeparator2 = new JSeparator();
					getContentPane().add(jSeparator2, new GridBagConstraints(0, 7, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jSeparator3 = new JSeparator();
					getContentPane().add(jSeparator3, new GridBagConstraints(0, 9, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					panelTitleLabel = new JLabel();
					getContentPane().add(panelTitleLabel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					panelTitleLabel.setText("Selected Event Details");
					panelTitleLabel.setFont(new java.awt.Font("Segoe UI",1,16));
					
				}
				{
					invitatiList = new JList<String>();
					invitatiList.setBackground(Color.WHITE);

					jScrollPane = new JScrollPane(invitatiList);
					
					getContentPane().add(jScrollPane, new GridBagConstraints(1, 10, 1, 4, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jSeparator4 = new JSeparator();
					getContentPane().add(jSeparator4, new GridBagConstraints(0, 14, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					deleteButton = new JButton();
					getContentPane().add(deleteButton, new GridBagConstraints(0, 15, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					deleteButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("defaultImages/deleteButton.png")));
					deleteButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

				}
			}
			this.setSize(314, 351);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void addListener()
	{
		deleteButton.addMouseListener(new MouseAdapter() 
		{

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				
				String id = event.getCalendarEntry().getId();
				
				GuiEvent guiEvent = HandlerEvent.createDeleteEvent(event.getCalendarEntry());
				
				HandlerView.getAgent().postGuiEvent(guiEvent);
				
				
//				GoogleCalendarCalls g = Broker.getCalendarService();
//				g.removeEvent(id);
				
				
//				TEST FUNZIONI in CALENDARCALLs
//				System.out.println("111111111111"+g.getEvent(id));
//				
//				
//				EventWho ew = new EventWho();
//				ew.setEmail("paolo@firmo.net");
//				event.getCalendarEntry().addParticipant(ew);
//				g.updateEvent(event.getCalendarEntry());
//				System.out.println("22222222222222"+g.getEvent(id).getParticipants().get(0).getEmail());
				
				
				
				HandlerView.getBroker().notifyObserversAt(HandlerView.getMenupanel().getAgendPanel(), id);
				
				setVisible(false);
				
				
								
			}
			
			
			
		});
	}

	private void setFieldsFromCalendarEvent(MyEvent entry)
	{
		event = entry;
		
		if(entry != null)
		{
			titoloField.setText(entry.getCalendarEntry().getTitle().getPlainText());

			doveField.setText(entry.getCalendarEntry().getLocations().get(0).getValueString());

			TextContent content = (TextContent) entry.getCalendarEntry().getContent();
			descrizioneTextArea.setText(content.getContent().getPlainText());
			descrizioneTextArea.setEditable(false);
			descrizioneTextArea.setBackground(null);

			String startTime = entry.getCalendarEntry().getTimes().get(0).getStartTime().toString();
			String endTime = entry.getCalendarEntry().getTimes().get(0).getEndTime().toString();

			System.out.println(startTime +"   "+ endTime);
			details.getDataInizioField().setText(startTime.substring(0, 10));
			details.getDataFineField().setText(endTime.substring(0, 10));

			details.getOrarioInizioField().setText(startTime.substring(11,16));
			details.getOrarioFineField().setText(endTime.substring(11,16));

			List<EventWho> invitati = new ArrayList<EventWho>();
			invitati = entry.getCalendarEntry().getParticipants();

			String[] invitatiArray = new String[invitati.size()];
			
			if(invitati.size() != 0)
			{
				for(int i=0; i<invitati.size(); i++)
					if( invitati.get(i).getEmail() != null)
						invitatiArray[i] = invitati.get(i).getEmail();
				
				setInvitatiListModel(new DefaultComboBoxModel<String>(invitatiArray));
				invitatiList.setModel(getInvitatiListModel());
				labelInvitati.setText("Invitati : "+invitati.size()+"  ");
		
			}
		}
	}

	public ListModel<String> getInvitatiListModel() {
		return invitatiListModel;
	}

	public void setInvitatiListModel(ListModel invitatiListModel) {
		this.invitatiListModel = invitatiListModel;
	}
}
